//
//  TTScreeningView.m
//  TaoTuo
//
//  Created by admin on 2019/11/26.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTScreeningView.h"
#import "LLTagView.h"
#import "TTScopeView.h"

@interface TTScreeningView ()
{
    LLTagConfig *tageConfig;
    LLTagConfig *minTageConfig;
    NSString *finishedProductKey;   //成品
    NSString *colorKey;   // 颜色
    NSString *jdKey;      // 净度
    NSString *cutKey;    // 切工
    NSArray *colorsArray;
    NSArray *jdArray;
    NSArray *cutArray;
}

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIView *screeningBgView;
@property (nonatomic, strong) UILabel *screeningLabel;

@property (nonatomic, strong) UILabel *kuanshiLabel;
@property (nonatomic, strong) LLTagView *kuanshiTageView;

@property (nonatomic, strong) UILabel *dingzhiLabel;
@property (nonatomic, strong) LLTagView *dingzhiTageView;

@property (nonatomic, strong) UILabel *productLabel;
@property (nonatomic, strong) LLTagView *productTageView;

@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) TTScopeView *priceScopeView;

//@property (nonatomic, strong) UILabel *stoneSizeLabel;
//@property (nonatomic, strong) TTScopeView *stoneSizeScopeView;

@property (nonatomic, strong) UILabel *colorLabel;
@property (nonatomic, strong) LLTagView *colorTageView;

@property (nonatomic, strong) UILabel *jingduLabel;
@property (nonatomic, strong) LLTagView *jingduTageView;

@property (nonatomic, strong) UILabel *cutLabel;
@property (nonatomic, strong) LLTagView *cutTageView;

@property (nonatomic, strong) NSMutableArray *tagViewsArray;

@property (nonatomic, strong) UIButton *resetBtn;   // 重置
@property (nonatomic, strong) UIButton *confirmBtn;   // 确定

@property (nonatomic, strong) NSMutableDictionary *dic;     // 参数

@end

@implementation TTScreeningView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.dic = [NSMutableDictionary dictionary];
        
        finishedProductKey = @"param.finishedProduct";
        colorKey = @"param.sStoneColor";
        jdKey = @"param.sStoneClarity";
        cutKey = @"param.sStoneCut";
        colorsArray = @[@"D-E", @"F-G", @"H", @"I-J", @"K-L", @"M-N", @"<N", @"黑色", @"白色"];
        jdArray = @[@"LC", @"VVS", @"VS", @"SI", @"P"];
        cutArray = @[@"十二心十二箭", @"十心十箭", @"八心八箭", @"很好"];
        
        tageConfig = [[LLTagConfig alloc] init];
        tageConfig.itemHeight = 27;
        tageConfig.minItemSpace = 5;
        tageConfig.minLineSpace = 5;
        tageConfig.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
        tageConfig.tagNormalbackgroundColor = UIColorHex(F0F4F7);
        tageConfig.tagSelectbackgroundColor = kMainColor;
        tageConfig.cornerRadius = 5;
        
        
        minTageConfig = [[LLTagConfig alloc] init];
        minTageConfig.itemHeight = 18;
        minTageConfig.minItemSpace = 5;
        minTageConfig.minLineSpace = 5;
        minTageConfig.fontSize = 10;
        minTageConfig.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
        minTageConfig.tagNormalbackgroundColor = UIColorHex(F0F4F7);
        minTageConfig.tagSelectbackgroundColor = kMainColor;
        minTageConfig.cornerRadius = 5;
        
        [self screeningLabel];
        [self resetBtn];
        [self confirmBtn];
        
        [self setNeedHiddenViews:YES];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(confirmBtnClick) name:@"dismissActionNotify" object:nil];
        
    }
    return self;
}

///重置
- (void)resetView
{
    if (self.resetBlock) {
        self.resetBlock(self.dic);
    }
    
    for (LLTagView *tagView in self.tagViewsArray) {
        [tagView resetView];
    }
    
    [self.priceScopeView resetView];
    [self setNeedHiddenViews:YES];
    
    [self.dic removeAllObjects];
}

///重置按钮
- (void)resetBtnClick
{
    [self resetView];
}

///确认
- (void)confirmBtnClick
{
    if (self.priceScopeView.minTF.text.length > 0) {
        [self.dic setObject:self.priceScopeView.minTF.text forKey:@"param.priceMin"];
    } else {
        [self.dic removeObjectForKey:@"param.priceMin"];
    }
    
    if (self.priceScopeView.maxTF.text.length > 0) {
        [self.dic setObject:self.priceScopeView.maxTF.text forKey:@"param.priceMax"];
    } else {
           [self.dic removeObjectForKey:@"param.priceMax"];
       }
    
    if (self.confirmBlock) {
        self.confirmBlock(self.dic);
    }
    [LLAnimationAlertView dismissView:self];
}

- (void)setSelectIndex:(NSInteger)index
{
    [self.productTageView setDefaultSelectIndex:index];
    
    [self setNeedHiddenViews:index == 1];
    NSInteger i = index == 0 ? 2 : 1;
    [self.dic setObject:@(i) forKey:finishedProductKey];
}

- (void)setSelectItemsWithDic:(NSDictionary *)paramsDic
{
    NSArray *keys = paramsDic.allKeys;
    // 产品
    if ([keys containsObject:finishedProductKey]) {
        if ([paramsDic[finishedProductKey] intValue] == 1) {
            [self setSelectIndex:1];
        } else if ([paramsDic[finishedProductKey] intValue] == 2) {
            [self setSelectIndex:0];
        }
    }
    // 颜色
    NSMutableArray *colorsIndexs = [self getDefaultSelectedIndexsWithParamsDic:paramsDic key:colorKey array:colorsArray];
    if (colorsIndexs != nil) {
        [self.colorTageView setDefaultSelectIndexs:colorsIndexs];
    }

    // 净度
    NSMutableArray *jdIndexs = [self getDefaultSelectedIndexsWithParamsDic:paramsDic key:jdKey array:jdArray];
    if (jdIndexs != nil) {
        [self.jingduTageView setDefaultSelectIndexs:jdIndexs];
    }

    // 切工
    NSMutableArray *cutIndexs = [self getDefaultSelectedIndexsWithParamsDic:paramsDic key:cutKey array:cutArray];
    if (cutIndexs != nil) {
        [self.cutTageView setDefaultSelectIndexs:cutIndexs];
    }
    
}

- (NSMutableArray *)getDefaultSelectedIndexsWithParamsDic:(NSDictionary *)paramsDic key:(NSString *)key array:(NSArray *)array
{
    NSArray *keys = paramsDic.allKeys;
    NSMutableArray *indexs = [NSMutableArray array];
    if ([keys containsObject:key]) {
        NSString *value = paramsDic[key];
        NSArray *valueArray = [value componentsSeparatedByString:@","];
        for (int i = 0; i < valueArray.count; i++) {
            for (int j = 0; j < array.count; j++) {
                if ([array[j] isEqualToString:valueArray[i]]) {
                    [indexs addObject:@(j)];
                    break;
                }
            }
        }
        return indexs;
    }
    return nil;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.dingzhiLabel.top = self.kuanshiTageView.bottom;
    self.dingzhiTageView.top = self.dingzhiLabel.bottom;
    
    self.productLabel.top = self.dingzhiTageView.bottom;
    self.productTageView.top = self.productLabel.bottom;
    
    self.priceLabel.top = self.productTageView.bottom;
    self.priceScopeView.top = self.priceLabel.bottom;
    
//    self.stoneSizeLabel.top = self.priceScopeView.bottom;
//    self.stoneSizeScopeView.top = self.stoneSizeLabel.bottom;
    
    self.colorLabel.top = self.priceScopeView.bottom;
    self.colorTageView.top = self.colorLabel.bottom;
    
    self.jingduLabel.top = self.colorTageView.bottom;
    self.jingduTageView.top = self.jingduLabel.bottom;
    
    self.cutLabel.top = self.jingduTageView.bottom;
    self.cutTageView.top = self.cutLabel.bottom;
    
    self.scrollView.contentSize = CGSizeMake(self.width, self.cutTageView.bottom + 50);
    
}


///需要隐藏的视图
- (void)setNeedHiddenViews:(BOOL)hidden
{
    self.colorLabel.hidden = hidden;
    self.colorTageView.hidden = hidden;
    self.jingduLabel.hidden = hidden;
    self.jingduTageView.hidden = hidden;
    self.cutLabel.hidden = hidden;
    self.cutTageView.hidden = hidden;
    
    if (hidden) {
        [self.dic removeObjectForKey:@"param.sStoneColor"];
        [self.dic removeObjectForKey:@"param.sStoneClarity"];
        [self.dic removeObjectForKey:@"param.sStoneCut"];
        [self.colorTageView resetView];
        [self.jingduTageView resetView];
        [self.cutTageView resetView];
    }
}


- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height - 50)];
        _scrollView.showsVerticalScrollIndicator = NO;
        [self addSubview:_scrollView];
    }
    return _scrollView;
}

- (UIView *)screeningBgView
{
    if (!_screeningBgView) {
        _screeningBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, StatusBarAndNavBar_Height)];
        _screeningBgView.backgroundColor = UIColorHex(F0F4F7);
        [self.scrollView addSubview:_screeningBgView];
    }
    return _screeningBgView;
}

- (UILabel *)screeningLabel
{
    if (!_screeningLabel) {
        _screeningLabel = [UILabel labelWithTitle:@"筛选" color:UIColorHex(333333) fontSize:18];
        _screeningLabel.frame = CGRectMake(10, kStatusBarHeight, 100, 44);
        [self.screeningBgView addSubview:_screeningLabel];
    }
    return _screeningLabel;
}

- (UILabel *)kuanshiLabel
{
    if (!_kuanshiLabel) {
        _kuanshiLabel = [UILabel labelWithTitle:@"款式" color:UIColorHex(333333) fontSize:15];
        _kuanshiLabel.frame = CGRectMake(10, self.screeningBgView.bottom + 6, 100, 30);
        [self.scrollView addSubview:_kuanshiLabel];
    }
    return _kuanshiLabel;
}

- (LLTagView *)kuanshiTageView
{
    if (!_kuanshiTageView) {
        _kuanshiTageView = [[LLTagView alloc] initWithFrame:CGRectMake(0, self.kuanshiLabel.bottom, self.width, 40) dataArray:@[@"经典款", @"简约款", @"轻奢款", @"豪华款", @"小清新"] selectItems:^(NSArray * _Nonnull selectArray) {
            if (selectArray.count > 0) {
                NSString *str = [selectArray componentsJoinedByString:@","];
                [self.dic setObject:str forKey:@"param.largeStyles"];
            } else {
                [self.dic removeObjectForKey:@"param.largeStyles"];
            }
        }];
        _kuanshiTageView.canCancelSelect = YES;
        _kuanshiTageView.allowMutiSelect = YES;
        _kuanshiTageView.config = tageConfig;
        [self setNeedsLayout];
        [self.scrollView addSubview:_kuanshiTageView];
        [self.tagViewsArray addObject:_kuanshiTageView];
    }
    return _kuanshiTageView;
}

- (UILabel *)dingzhiLabel
{
    if (!_dingzhiLabel) {
        _dingzhiLabel = [UILabel labelWithTitle:@"定制" color:UIColorHex(333333) fontSize:15];
        _dingzhiLabel.frame = CGRectMake(10, self.kuanshiTageView.bottom + 6, 100, 30);
        [self.scrollView addSubview:_dingzhiLabel];
    }
    return _dingzhiLabel;
}

- (LLTagView *)dingzhiTageView
{
    if (!_dingzhiTageView) {
        _dingzhiTageView = [[LLTagView alloc] initWithFrame:CGRectMake(0, self.dingzhiLabel.bottom, self.width, 40) dataArray:@[@"可定制"] selectItems:^(NSArray * _Nonnull selectArray) {
            if (selectArray.count > 0) {
                [self.dic setObject:@1 forKey:@"param.styleType"];
            } else {
                [self.dic removeObjectForKey:@"param.styleType"];
            }
        }];
        _dingzhiTageView.canCancelSelect = YES;
        _dingzhiTageView.allowMutiSelect = YES;
        _dingzhiTageView.config = tageConfig;
        [self setNeedsLayout];
        [self.scrollView addSubview:_dingzhiTageView];
        [self.tagViewsArray addObject:_dingzhiTageView];
    }
    return _dingzhiTageView;
}

- (UILabel *)productLabel
{
    if (!_productLabel) {
        _productLabel = [UILabel labelWithTitle:@"产品" color:UIColorHex(333333) fontSize:15];
        _productLabel.frame = CGRectMake(10, self.dingzhiTageView.bottom + 6, 100, 30);
        [self.scrollView addSubview:_productLabel];
    }
    return _productLabel;
}


- (LLTagView *)productTageView
{
    if (!_productTageView) {
        _productTageView = [[LLTagView alloc] initWithFrame:CGRectMake(0, self.productLabel.bottom, self.width, 40) dataArray:@[@"成品", @"空托"] selectItems:^(NSArray * _Nonnull selectArray) {
            NSLog(@">> sele = %@", selectArray);
            if (selectArray.count == 0) {
                [self setNeedHiddenViews:YES];
                [self.dic removeObjectForKey:@"param.finishedProduct"];
            } else {
                [self setNeedHiddenViews:[selectArray.firstObject isEqualToString:@"空托"]];
                NSInteger i = [selectArray.firstObject isEqualToString:@"成品"] ? 2 : 1;
                [self.dic setObject:@(i) forKey:@"param.finishedProduct"];
            }
        }];
        _productTageView.canCancelSelect = YES;
        _productTageView.config = tageConfig;
        [self.scrollView addSubview:_productTageView];
        [self.tagViewsArray addObject:_productTageView];
    }
    return _productTageView;
}

- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [UILabel labelWithTitle:@"价格区间" color:UIColorHex(666666) fontSize:11];
        _priceLabel.frame = CGRectMake(10, self.productTageView.bottom, 150, 20);
        [self.scrollView addSubview:_priceLabel];
    }
    return _priceLabel;
}

- (TTScopeView *)priceScopeView
{
    if (!_priceScopeView) {
        _priceScopeView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, self.priceLabel.bottom, self.width, 37) title:@"" hiddenTextField:NO];
        _priceScopeView.minTFLeftSpace = 50;
        [self.scrollView addSubview:_priceScopeView];
    }
    return _priceScopeView;
}

//- (UILabel *)stoneSizeLabel
//{
//    if (!_stoneSizeLabel) {
//        _stoneSizeLabel = [UILabel labelWithTitle:@"主石大小" color:UIColorHex(666666) fontSize:11];
//        _stoneSizeLabel.frame = CGRectMake(10, self.priceScopeView.bottom + 5, 150, 15);
//        [self.scrollView addSubview:_stoneSizeLabel];
//    }
//    return _stoneSizeLabel;
//}
//
//- (TTScopeView *)stoneSizeScopeView
//{
//    if (!_stoneSizeScopeView) {
//        _stoneSizeScopeView = [[TTScopeView alloc] initWithFrame:CGRectMake(0, self.stoneSizeLabel.bottom, self.width, 37) title:@"" hiddenTextField:NO];
//        _stoneSizeScopeView.minTFLeftSpace = 50;
//        [self.scrollView addSubview:_stoneSizeScopeView];
//    }
//    return _stoneSizeScopeView;
//}


- (UILabel *)colorLabel
{
    if (!_colorLabel) {
        _colorLabel = [UILabel labelWithTitle:@"颜色" color:UIColorHex(666666) fontSize:11];
        _colorLabel.frame = CGRectMake(10, self.priceScopeView.bottom + 6, 100, 20);
        [self.scrollView addSubview:_colorLabel];
    }
    return _colorLabel;
}

- (LLTagView *)colorTageView
{
    if (!_colorTageView) {
        _colorTageView = [[LLTagView alloc] initWithFrame:CGRectMake(0, self.colorLabel.bottom, self.width, 40) dataArray:colorsArray selectItems:^(NSArray * _Nonnull selectArray) {
            if (selectArray.count > 0) {
                NSString *str = [selectArray componentsJoinedByString:@","];
                [self.dic setObject:str forKey:@"param.sStoneColor"];
            } else {
                [self.dic removeObjectForKey:@"param.sStoneColor"];
            }
        } config:minTageConfig];
        _colorTageView.canCancelSelect = YES;
        _colorTageView.allowMutiSelect = YES;
        [self.scrollView addSubview:_colorTageView];
        [self.tagViewsArray addObject:_colorTageView];
    }
    return _colorTageView;
}


- (UILabel *)jingduLabel
{
    if (!_jingduLabel) {
        _jingduLabel = [UILabel labelWithTitle:@"净度" color:UIColorHex(666666) fontSize:11];
        _jingduLabel.frame = CGRectMake(10, self.colorTageView.bottom + 6, 100, 20);
        [self.scrollView addSubview:_jingduLabel];
    }
    return _jingduLabel;
}

- (LLTagView *)jingduTageView
{
    if (!_jingduTageView) {
        _jingduTageView = [[LLTagView alloc] initWithFrame:CGRectMake(0, self.jingduLabel.bottom, self.width, 40) dataArray:jdArray selectItems:^(NSArray * _Nonnull selectArray) {
            if (selectArray.count > 0) {
                NSString *str = [selectArray componentsJoinedByString:@","];
                [self.dic setObject:str forKey:@"param.sStoneClarity"];
            } else {
                [self.dic removeObjectForKey:@"param.sStoneClarity"];
            }
        } config:minTageConfig];
        _jingduTageView.canCancelSelect = YES;
        _jingduTageView.allowMutiSelect = YES;
        [self.scrollView addSubview:_jingduTageView];
        [self.tagViewsArray addObject:_jingduTageView];
    }
    return _jingduTageView;
}

- (UILabel *)cutLabel
{
    if (!_cutLabel) {
        _cutLabel = [UILabel labelWithTitle:@"切工" color:UIColorHex(666666) fontSize:11];
        _cutLabel.frame = CGRectMake(10, self.jingduTageView.bottom + 6, 100, 20);
        [self.scrollView addSubview:_cutLabel];
    }
    return _cutLabel;
}

- (LLTagView *)cutTageView
{
    if (!_cutTageView) {
        _cutTageView = [[LLTagView alloc] initWithFrame:CGRectMake(0, self.cutLabel.bottom, self.width, 40) dataArray:cutArray selectItems:^(NSArray * _Nonnull selectArray) {
            if (selectArray.count > 0) {
                NSString *str = [selectArray componentsJoinedByString:@","];
                [self.dic setObject:str forKey:@"param.sStoneCut"];
            } else {
                [self.dic removeObjectForKey:@"param.sStoneCut"];
            }
        } config:minTageConfig];
        _cutTageView.canCancelSelect = YES;
        _cutTageView.allowMutiSelect = YES;
        _cutTageView.needReloadView = self;
        [self.scrollView addSubview:_cutTageView];
        [self.tagViewsArray addObject:_cutTageView];
    }
    return _cutTageView;
}

- (UIButton *)resetBtn
{
    if (!_resetBtn) {
        _resetBtn = [UIButton createButtonWithTitle:@"重置" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:20] backgroundColor:UIColorHex(404469) target:self action:@selector(resetBtnClick)];
        _resetBtn.frame = CGRectMake(0, self.bottom - 50, self.width * 0.5, 50);
        [self addSubview:_resetBtn];
    }
    return _resetBtn;
}

- (UIButton *)confirmBtn
{
    if (!_confirmBtn) {
        _confirmBtn = [UIButton createButtonWithTitle:@"确认" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:20] backgroundColor:kMainColor target:self action:@selector(confirmBtnClick)];
        _confirmBtn.frame = CGRectMake(self.resetBtn.right, self.resetBtn.top, self.resetBtn.width, 50);
        [self addSubview:_confirmBtn];
    }
    return _confirmBtn;
}

- (NSMutableArray *)tagViewsArray
{
    if (!_tagViewsArray) {
        _tagViewsArray = [NSMutableArray array];
    }
    return _tagViewsArray;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
